package org.dreamfly.core.service.impl;

import org.dreamfly.core.action.FileActionSupport;
import org.dreamfly.core.bean.SzActivity;
import org.dreamfly.core.dao.ActivityDAO;
import org.dreamfly.core.page.PageInfo;
import org.dreamfly.core.page.PageResultSet;
import org.dreamfly.core.service.ActivityService;
import org.dreamfly.core.util.UrlGetter;

import java.util.Iterator;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Gin
 * Date: 2010-10-10
 * Time: 0:57:38
 * To change this template use File | Settings | File Templates.
 */
public class ActivityServiceImpl implements ActivityService {
    private ActivityDAO activityDAO;
    private UrlGetter urlGetter;

    @Override
    public void saveOrUpdate(SzActivity activity) {
        activityDAO.saveOrUpdate(activity);
    }

    public void setActivityDAO(ActivityDAO activityDAO) {
        this.activityDAO = activityDAO;
    }

    @Override
    public PageResultSet<SzActivity> normalSearch(SzActivity activity, Long pageSize, Long page, String servername) {
        PageInfo pageInfo = new PageInfo(activityDAO.findCount(SzActivity.class, activity.getTitle()), pageSize, page);

        List<SzActivity> list = activityDAO.find(SzActivity.class,
                activity.getTitle(),
                Integer.parseInt(pageInfo.getBeginIndex().toString()),
                Integer.parseInt(pageInfo.getPageSize().toString()), servername);
        if (null != list) {
            Iterator iterator = list.iterator();
            while (iterator.hasNext()) {
                SzActivity temp = (SzActivity) iterator.next();
                temp.setMinPictureUrl(urlGetter.getRemoteUrl(temp.getMinPictureUrl(), temp.getServer()));
            }
        }
        PageResultSet<SzActivity> pageResultSet = new PageResultSet<SzActivity>();
        pageResultSet.setList(list);
        pageResultSet.setPageInfo(pageInfo);
        return pageResultSet;
    }

    @Override
    public void delete(Long id) {
        SzActivity a = activityDAO.find(SzActivity.class, id);
        FileActionSupport fas = new FileActionSupport();
        String[] filePaths = {a.getActivityUrl(), a.getMinPictureUrl()};
        fas.setFilePaths(filePaths);
        try {
            fas.delete();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        activityDAO.delete(a);
    }

    @Override
    public SzActivity findDetail(Long id) {
        SzActivity activity = activityDAO.find(SzActivity.class, id);
        activity.setMinPictureUrl(urlGetter.getRemoteUrl(activity.getMinPictureUrl(),activity.getServer()));
        return activity;
    }

    @Override
    public void addActionInfo(SzActivity activity) {
        activityDAO.saveInfo(activity);
    }

    @Override
    public void modifyActionInfo(SzActivity activity) {
        activityDAO.modifyInfo(activity);
    }

    public void setUrlGetter(UrlGetter urlGetter) {
        this.urlGetter = urlGetter;
    }
}
